<script>
import roleList from "../PermissionManagement/RoleList.vue";

export default {
  name: "EmployeeList",
  computed: {
    /*stateBoolean(val) {
      return val === 1;
    }*/
  },
  data() {
    return {
      queryInfo: {
        keyword: '',
        currentPage: 1,
        pageSize: 8
      },
      total: 0,
      employeeList: [
      ],
      listLoading: false,
      sels: [],
      editFormVisible: false,
      editFormRules: [
        {required: true, message: '请输入部门名称', trigger: 'blur'},
        {required: true, message: '请输入部门简介', trigger: 'blur'},
        {required: true, message: '请输入部门经理', trigger: 'blur'},
        {required: true, message: '请输入父级部门', trigger: 'blur'},
        {required: true, message: '请输入部门状态', trigger: 'blur'},
      ],
      editForm: {
        id: '',
        username: '',
        headImage: '',
        email: '',
        age: '',
        department: {
          id: '',
          name: '',
          intro: '',
          state: '',
        },
        role: {
          id: '',
          name: '',
          sn: ''
        }
      },
      editLoading: false,
      departmentTree: [],
      departmentList: [],
      roleList: [],
      uploadVisible: false,
      errorEmployeeList: [],
      errorDataVisible: false
    }
  },
  methods: {
    getEmployeeList() {
      this.$http.post("/employee", this.queryInfo).then(res => {
        // console.log(res)
        if (res.data.success) {
          this.employeeList = res.data.data.list
          // console.log(this.employeeList)
          this.total = res.data.data.total
        } else {
          this.$message({
            message: res.data.msg,
            type: "error",
            showClose: true
          })
        }
      }).catch(err => {
        this.$message({
          message: "请求异常",
          type: "error",
          showClose: true
        })
      }).finally(() => {
        this.listLoading = false;
      })
    },
    queryEmployee() {
      this.getEmployeeList();
    },
    handleAdd() {
      this.editFormVisible = true;
      this.$refs["editForm"].resetFields();
      /*this.editForm = {
        id: '',
        username: '',
        headImage: '',
        email: '',
        age: '',
        department: {
          id: '',
          name: '',
          intro: '',
          state: '',
        },
        role: {
          id: '',
          name: '',
          sn: ''
        }
      };*/
      this.queryDepartmentList();
    },
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;
      this.getEmployeeList();
    },
    selsChange(sels) {
      this.sels = sels
    },
    batchRemove() {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let ids = this.sels.map(item => item.id);
        // console.log(ids)
        this.$http.patch("/employee", ids).then(res => {
          if (res.data.success) {
            this.$message({
              message: res.data.msg,
              type: "success",
              showClose: true
            })
            this.getEmployeeList();
          }
        }).catch(err => {
          this.$message({
            message: "请求异常",
            type: "error",
            showClose: true
          })
        }).finally(() => {
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },
    handleCurrentChange(val) {
      this.queryInfo.currentPage = val;
      this.getEmployeeList();
    },
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.getRoleList();
      this.queryDepartmentList();

      this.editForm = Object.assign({}, row);
      if (!row.role) {
        this.editForm.role = {
          id: null,
          name: ''
        }
      }
    },
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/employee/" + row.id).then(res => {
          if (res.data.success) {
            this.$message({
              message: res.data.msg,
              type: "success",
              showClose: true
            })
            this.getEmployeeList();
          }
        }).catch(err => {
          this.$message({
            message: "请求异常",
            type: "error",
            showClose: true
          })
        }).finally(() => {
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },
    editSubmit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.editLoading = true;
          this.$http.put("/employee", this.editForm).then(res => {
            if (res.data.success) {
              this.editFormVisible = false;
              this.$message({
                message: res.data.msg,
                type: "success",
                showClose: true
              })
              this.getEmployeeList();
            }
          }).catch(err => {
            this.$message({
              message: "请求异常",
              type: "error",
              showClose: true
            })
          }).finally(() => {
            this.editLoading = false;
          })
        }
      })
    },
    queryEmployeeTree(id) {
      this.$http.get("/employee/parents/" + id).then(res =>{
        if (res.data.success) {
          this.departmentTree = res.data.data;
        }
      }).catch(error => {
        this.$message({
          message: "父级部门列表获取失败",
          type: "error",
          showClose: true
        })
      })
    },
    queryDepartmentList() {
      this.$http.get("/department").then(res => {
        if (res.data.success) {
          this.departmentList = res.data.data;
        }
      }).catch(error => {
        this.$message({
          message: "部门经理列表获取失败",
          type: "error",
          showClose: true
        })
      })
    },
    getRoleList() {
      this.$http.get("/role").then(res => {
        this.roleList = res.data.data
      })
    },
    handleUploadClose(){
      this.uploadVisible = false;
    },
    handleUploadSuccess(response, file, fileList) {
      if (response.success) {
        this.$message({
          message: response.msg,
          type: "success",
          showClose: true
        })
        this.getEmployeeList();
      } else {
        console.log(response);
        this.$message({
          message: response.msg,
          type: "error",
          showClose: true
        })
        this.getEmployeeList();

        this.errorEmployeeList = response.data;
        this.errorDataVisible = true;
      }
      this.uploadVisible = false;
    },
    handelErrorDataClose() {
      this.errorDataVisible = false;
    }
  },
  mounted() {
    this.getEmployeeList()
  }
}
</script>

<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="queryInfo.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryEmployee">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="uploadVisible = true">导入员工</el-button>
        </el-form-item>
        <el-form-item>
<!--          <el-button type="text" @click="window.location.href = '/employee/export'">导出员工</el-button>-->
          <a :href="'http://localhost:8080/employee/export?keyword=' + queryInfo.keyword" target="_self">
            <el-button type="primary">导出员工</el-button>
          </a>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="employeeList" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              max-height="450px"
              style="width: 100%;">
      <el-table-column type="selection" width="55"/>
      <el-table-column type="index" width="60"/>
      <el-table-column prop="username" label="员工姓名" width="120" sortable/>
      <el-table-column prop="email" label="邮箱" width="240" sortable/>
      <el-table-column prop="role.name" label="角色" width="240" sortable/>
      <el-table-column prop="age" label="年龄" width="120" sortable/>
      <el-table-column prop="department.name" label="所属部门" width="100" sortable/>
      <el-table-column label="操作" width="150">
        <template v-slot="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.currentPage"
          :page-sizes="[3, 5, 8, 10, 20]"
          :page-size="8"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editFormVisible">
<!--      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">-->
      <el-form :model="editForm" label-width="80px" ref="editForm">
        <el-form-item label="员工姓名">
          <el-input v-model="editForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工角色" prop="role.id">
          <el-select v-model="editForm.role.id" placeholder="请选择">
            <el-option
                v-for="item in this.roleList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.intro }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工邮箱">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工年龄">
          <el-input type="number" v-model="editForm.age" auto-complete="off" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="department.id">
          <el-select v-model="editForm.department.id" placeholder="请选择">
            <el-option
                v-for="item in departmentList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.intro }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

<!--    文件上传界面-->
    <el-dialog
        title="提示"
        :visible.sync="uploadVisible"
        width="40%"
        :before-close="handleUploadClose">
      <el-upload
          class="upload-demo"
          drag
          action="/api/employee/import"
          :on-success="handleUploadSuccess"
          multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传.xlsx文件，且不超过10M</div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
<!--    <el-button @click="uploadVisible = false">取 消</el-button>
    <el-button type="primary" @click="">确 定</el-button>-->
  </span>
    </el-dialog>

    <el-dialog
        title="错误数据"
        :visible.sync="errorDataVisible"
        width="80%"
        :before-close="handelErrorDataClose">
      <el-table :data="errorEmployeeList" highlight-current-row
                max-height="450px"
                style="width: 100%;">
<!--        <el-table-column type="selection" width="55"/>-->
        <el-table-column type="index" width="60"/>
        <el-table-column prop="username" label="员工姓名" width="120" sortable/>
        <el-table-column prop="email" label="邮箱" width="240" sortable/>
        <el-table-column prop="role.name" label="角色" width="240" sortable/>
        <el-table-column prop="age" label="年龄" width="120" sortable/>
        <el-table-column prop="department.name" label="所属部门" width="100" sortable/>
      </el-table>
      <span slot="footer" class="dialog-footer">
<!--    <el-button @click="errorDataVisible = false">取 消</el-button>-->
    <el-button type="primary" @click="errorDataVisible = false">确 定</el-button>
    <a href="http://localhost:8080/employee/error" target="_self">
      <el-button type="primary">下载错误数据</el-button>
    </a>
  </span>
    </el-dialog>
  </section>
</template>

<style scoped>

</style>
